package com.superjson.showpro.controller.core;

import com.alibaba.fastjson.JSONObject;
import com.beantools.constants.SingleChar;
import com.superjson.showpro.shared.result.Result;
import com.superjson.showpro.shared.result.ResultUtil;
import org.springframework.util.CollectionUtils;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.RestController;

import java.util.LinkedList;
import java.util.List;
import java.util.Set;

import static com.beantools.constants.SingleChar.SERIES_COMMA_OP;
import static com.beantools.constants.SingleChar.SIGN_COLON_OP;
import static com.superjson.showpro.shared.util.MethodUtil.getObject;
import static org.apache.commons.lang3.StringUtils.isBlank;

/**
 * 这是获取枚举类型的类
 */
@RestController("coreEnumController")
@RequestMapping("/core/enum/")
public class CoreEnumController {

    /**
     * 返回指定类型的枚举，比如com.superjson.showpro.shared.enums
     */
    @RequestMapping(value = "list", method = RequestMethod.GET)
    public Result listEnums(String path) {
        if (isBlank(path)) {
            return null;
        }
        JSONObject body = new JSONObject();
        if (path.contains(SERIES_COMMA_OP)) {
            String[] paths = path.split(SERIES_COMMA_OP);
            for (String var : paths) {
                Set set = getObject(var);
                if (CollectionUtils.isEmpty(set)) {
                    continue;
                }
                body.put(var, set);
            }
            return ResultUtil.buildSuccess(body);
        }
        Set set = getObject(path);
        if (CollectionUtils.isEmpty(set)) {
            return  ResultUtil.buildSuccess();
        }
        body.put(path, getObject(path));
        return ResultUtil.buildSuccess(body);
    }

}
